package lambdademo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-10-10
 * Time: 18:46
 */
@FunctionalInterface
interface IA {
    void test();// 接口中允许有抽象方法

    default void test2() {
    }// default默认方法可以有具体的实现

    public static void test3() {
    }// 也可以有public关键字修饰
}


//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a, int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}

//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a, int b);
}
public class Test {
    public static void main10(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("hello", 3);
        map.put("world", 5);
        map.put("Joker", 2);

        //作用是对Map中的每个映射执行action指定的操作
        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key: " + s + " val: " + integer);
            }
        });
        System.out.println("=======");
        //精减：
        map.forEach((s, integer) -> System.out.println("key: " + s + " val: " + integer));
    }
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("world");
        list.add("hello");
        //该方法表示：对容器中的每个元素执行action指定的动作
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("=======");
        //精减：
        list.forEach(s -> System.out.println(s));
        //进行排序：
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //精减：
        list.sort(((o1, o2) -> o1.compareTo(o2)));

        System.out.println("=======");
        list.forEach(s -> System.out.println(s));
    }
    public static void main8(String[] args) {
        int count = 0;
        MoreParameterNoReturn moreParameterNoReturn = (int a, int b) -> {
            System.out.println(a + b);
            //count = 100;
        };
        count = 200;
        moreParameterNoReturn.test(100, 200);
        System.out.println(count);
    }
    public static void main7(String[] args) {
        final int a = 10;
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(a);
                return o1.compareTo(o2);
            }
        });
        //精减：
        PriorityQueue<Integer> priorityQueue2 = new PriorityQueue<>((Integer o1, Integer o2) -> o1.compareTo(o2));
        //再精简：
        PriorityQueue<Integer> priorityQueue3 = new PriorityQueue<>((o1, o2) -> o1.compareTo(o2));
    }
    public static void main6(String[] args) {
        MoreParameterReturn moreParameterReturn = (int a, int b) -> {return a + b;};
        //省略
        MoreParameterReturn moreParameterReturn2 = (a, b) -> a * b;

        System.out.println(moreParameterReturn.test(10, 20));// 30
        System.out.println(moreParameterReturn2.test(10, 20));// 200
    }
    public static void main5(String[] args) {
        OneParameterReturn oneParameterReturn = (int a) -> {return a;};
        //也可省略：
        OneParameterReturn oneParameterReturn2 = a -> a;

        System.out.println(oneParameterReturn.test(100));// 100
        System.out.println(oneParameterReturn2.test(200));// 200
    }
    public static void main4(String[] args) {
        NoParameterReturn noParameterReturn = () -> {return 10;};
        //省略：
        NoParameterReturn noParameterReturn2 = () -> 20;

        System.out.println(noParameterReturn.test());// 10
        System.out.println(noParameterReturn2.test());// 20
    }
    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn = (int a, int b) -> {
            System.out.println(a + b);
        };
        moreParameterNoReturn.test(10, 20);// 30
    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = new OneParameterNoReturn() {
            @Override
            public void test(int a) {
                System.out.println(a);
            }
        };
        oneParameterNoReturn.test(100);
        //语法精简
        OneParameterNoReturn oneParameterNoReturn2 = (int a) -> {System.out.println(a);};
        oneParameterNoReturn2.test(200);
        //只有一条语句的时候，括号、return、数据类型都可以省略
        OneParameterNoReturn oneParameterNoReturn3 = a -> System.out.println(a);
        oneParameterNoReturn3.test(300);
    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("重写test()方法！！！");
            }
        };//匿名内部类

        noParameterNoReturn.test();
        System.out.println("======");
        //语法精简
        NoParameterNoReturn noParameterNoReturn2 = () -> {System.out.println("重写test()方法！！！");};
        noParameterNoReturn2.test();
    }
}
